﻿using System;
using log4net;

namespace MyLib {
	/// <summary>
	/// 
	/// </summary>
	public static class ExceptionExtensions {
		/// <summary>
		/// 
		/// </summary>
		private static ILog m_logger = LogManager.GetLogger(typeof(ExceptionExtensions));

		/// <summary>
		/// 
		/// </summary>
		/// <param name="exception"></param>
		/// <returns></returns>
		public static string FormatMessage(this Exception exception) {
			if (exception == null)
				return string.Empty;

			while (true) {
				var iexception = exception.InnerException;
				if (iexception == null) {
					if (m_logger.IsWarnEnabled) {
						m_logger.Warn("Exception found.", exception);
					}

					if (exception is System.Data.Common.DbException)
						return Properties.Main.ErrorDbException;
					else if (exception is System.ApplicationException)
						return exception.Message;
					else
						return string.IsNullOrWhiteSpace(exception.Message) ? Properties.Main.UnknownException : exception.Message;
				} else {
					exception = iexception;
				}
			} // end of while.
		}
	} // end of ExceptionExtensions.
}
